package com.ynny.librarysystemmanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ynny.librarysystemmanager.entity.User;
import com.ynny.librarysystemmanager.entity.UserDTO;
import com.ynny.librarysystemmanager.mapper.UserMapper;
import com.ynny.librarysystemmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAll(User user) {

        List<User> users = userMapper.selectAll(user);

        return users;
    }

    @Override
    public User findById(Integer userId) {

        return userMapper.selectOne(userId);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int editUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public int disableUser(Integer useId , Integer status) {

        return userMapper.deleteUser(useId , status);
    }



    @Override
    public User login(String username, String password) {

        //检查用户名是否存在
        User user = new User();
        user.setUsername(username);

        User userByUserName = userMapper.selectUser(user);
        if (userByUserName == null){
            return null;
        }
        //检查用户名和密码是否正确
        user.setPassword(password);
        User userByUserNameAndPwd = userMapper.selectUser(user);

        if (userByUserNameAndPwd == null){
            return null;
        }
        //如果正确，返回用户信息
        return userByUserNameAndPwd;
    }

    //分页查询
    @Override
    public PageInfo page(UserDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        List<User> users = userMapper.selectAll(dto);

        PageInfo pageInfo = new PageInfo<>(users);

        return pageInfo;
    }



}
